package com.byronspace.controller;

import io.swagger.annotations.ApiOperation;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import news.utils.httpclient.ClientMethodUtils;
import news.utils.httpclient.HttpClientManger;
import news.utils.model.Weather;
import news.utils.parse.BaseParse;
import news.utils.util.LoggerUtils;

import org.apache.http.client.methods.HttpGet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.byronspace.searvice.WeatherService;
import com.byronspace.util.BlogProperties;
import com.byronspace.util.DownLoadFromUrl;

@RestController
public class WeatherController {
	@Autowired
	private JdbcTemplate jdbcTemplate;
	@Autowired
	private BlogProperties blogProperties;
	@Autowired
    private WeatherService weatherService;
	
	@ApiOperation(value = "获取天气列表", notes = "")
	@RequestMapping(value = "/weathers")
	public List<Map<String, Object>> getWeatherList() {
		String sql = "select * from weather";
		List<Map<String, Object>> list = jdbcTemplate.queryForList(sql);
		for (Map<String, Object> map : list) {
			Set<Entry<String, Object>> entries = map.entrySet();
			if (entries != null) {
				Iterator<Entry<String, Object>> iterator = entries.iterator();
				while (iterator.hasNext()) {
					Entry<String, Object> entry = (Entry<String, Object>) iterator
							.next();
					Object key = entry.getKey();
					Object value = entry.getValue();
					System.out.println(key + ":" + value);
				}
			}
		}
		List<Weather> weatherlist = weatherService.likeName("晴");
		return list;
	}

	@ApiOperation(value = "从网络获取天气列表编码与参数", notes = "")
	@RequestMapping(value = "/getWeathers")
	public List<Map<String, Object>>  getWeatherListFromWeb() throws Exception {
		List<Weather> weatherList = new ArrayList<Weather>();
		String url = "https://www.heweather.com/documents/condition";
		HttpGet get = new HttpGet(url);
		// 使用下载器下载页面元素
		String page = ClientMethodUtils.getContent(HttpClientManger
				.newInstance().getClient(), get);
		Map<String, String> map = new HashMap<>();
		for (int i = 2; i <= 52; i++) {
			map.clear();
			// 配置编码的xpath
			map.put("code","//*[@id=\"node-80\"]/div[1]/div/div/table/tbody/tr["+i+ "]/td[1]/text()");
			// 配置天气中文的xpath
			map.put("nameCn","//*[@id=\"node-80\"]/div[1]/div/div/table/tbody/tr["+i+"]/td[2]/text()");
			// 配置天气英文的xpath
			map.put("nameEn","//*[@id=\"node-80\"]/div[1]/div/div/table/tbody/tr["+i+ "]/td[3]/text()");
			// 配置天气icon名称的xpath
			map.put("nameIcon","//*[@id=\"node-80\"]/div[1]/div/div/table/tbody/tr["+i+"]/td[4]/a/text()");
			map.put("iconUrl", "//*[@id=\"node-80\"]/div[1]/div/div/table/tbody/tr["+i+"]/td[4]/a/@href");

			// 调用解析取解析页面数据
			Weather weatherPojo = BaseParse.parseObject(Weather.class, page,map);
			weatherList.add(weatherPojo);
			LoggerUtils.info(weatherPojo.toString());
		}
		//清空天气表数据
		String deleteTableSQL = "delete from weather ";
		jdbcTemplate.execute(deleteTableSQL);
		//插入天气数据
		String insertSQL= "INSERT INTO weather(code, nameCn,nameEn,nameIcon,iconUrl) VALUES ";
		String valueStr = "";
		for(int i=0;i<weatherList.size();i++){
			valueStr += "('"+weatherList.get(i).getCode()+"', '"+weatherList.get(i).getNameCn()
					+"','"+weatherList.get(i).getNameEn()+"','"+weatherList.get(i).getNameIcon()
					+"','"+weatherList.get(i).getIconUrl()+"')";
			if(i<weatherList.size()-1){
				valueStr+=",";
			}
		}
		insertSQL += valueStr;
		System.out.println(insertSQL);
		jdbcTemplate.execute(insertSQL);
		//返回插入后的数据
		String sql = "select * from weather";
		List<Map<String, Object>> list = jdbcTemplate.queryForList(sql);
		return list;
	}
	
	@ApiOperation(value = "下载天气图标", notes = "")
	@RequestMapping(value = "/downLoadWeatherIcons")
	public void downloadWeatherIcon() throws IOException{
		String savePath = blogProperties.getIconSavePath();
		String iconName= "";
		String downloadUrl = "";
		String sql = "select * from weather";
		List<Map<String, Object>> list = jdbcTemplate.queryForList(sql);
	
		for(int i=0;i<list.size();i++){	
			iconName= list.get(i).get("iconName").toString();
			downloadUrl = list.get(i).get("iconUrl").toString();
			LoggerUtils.info("iconName:"+iconName+";downloadUrl:"+downloadUrl+";savePath:"+savePath);
			DownLoadFromUrl.downLoadFromUrl(downloadUrl, iconName, savePath);
		}

	}
}
